package com.credithc.customers.manager.service.impl;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.exception.BsException;
import com.credithc.baseserv.core.service.impl.BaseServiceImpl;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.base.factory.DictionaryFactory;
import com.credithc.customers.base.mapper.AccountInfoStatisticMapper;
import com.credithc.customers.base.mapper.CustomerAccountMapper;
import com.credithc.customers.base.po.CustomerAccountPo;
import com.credithc.customers.base.service.ICacheService;
import com.credithc.customers.base.utils.CustomerUtils;
import com.credithc.customers.manager.mapper.CustomerAccountInfoMapper;
import com.credithc.customers.manager.ro.CustomerAccountInfoRo;
import com.credithc.customers.manager.service.CustomerAccountService;
import com.credithc.customers.manager.utils.StatsUtils;
import com.credithc.customers.manager.vo.AccountCountVo;
import com.credithc.customers.manager.vo.CustomerAccountVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CustomerAccountServiceImpl extends BaseServiceImpl<CustomerAccountPo, CustomerAccountInfoRo, CustomerAccountPo> implements CustomerAccountService {
    @Autowired
    CustomerAccountMapper customerAccountMapper;

    @Autowired
    private CustomerAccountInfoMapper customerAccountInfoMapper;

    @Autowired
    private AccountInfoStatisticMapper accountInfoStatisticMapper;

    @Autowired
    private ICacheService cacheService;

    @Override
    public List<AccountCountVo> getAccountCount(String customerNo) {
        try {
            CustomerAccountPo po = new CustomerAccountPo();
            if (StringUtils.isNotEmpty(customerNo)) {
                po.setCustomerNo(customerNo);
                List<CustomerAccountPo> list = customerAccountMapper.select(po);
                int availableCount = 0;
                int unavailableCount = 0;
                for (CustomerAccountPo c : list) {
                    if (c.getAccountStatus().equals(CustomerConstants.STATUS_ACCOUNT_AVAILABLE)) {
                        availableCount++;
                    } else {
                        unavailableCount++;
                    }
                }
                List<AccountCountVo> s = new ArrayList<>();
                s.add(new AccountCountVo(availableCount, "可用账户"));
                s.add(new AccountCountVo(unavailableCount, "不可用账户"));
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }
        return null;
    }

    @Override
    public PageInfo<CustomerAccountVo> getList(CustomerAccountInfoRo ro, Integer pageNo, Integer pageSize) {
        List<CustomerAccountVo> vos = new ArrayList<>();
        List<CustomerAccountPo> pos = new ArrayList<>();
        int count = 0;
        //如果查询只包括或含账户类型，账户状态，系统来源和时间则走以下下查询
        if (StringUtils.isBlank(ro.getCustomerNo()) && StringUtils.isBlank(ro.getCustomerName()) && StringUtils.isBlank(ro.getAccountNo())) {
            String createTimeStart = ro.getCreateTimeStart();
            String createTimeEnd = ro.getCreateTimeEnd();
            if (StringUtils.isBlank(ro.getCreateTimeEnd())){
                //未选取结束时间
                createTimeEnd = StatsUtils.getCurrentTime().substring(0, 10);
            }
            int countEndDay;
            String countStartSum = "0";
            String nextDate = StatsUtils.getPostDate(1, createTimeEnd);
            String endDate = StatsUtils.getPastDate(1, createTimeEnd.substring(0, 10));
            if (ro.getSystemSign() == null || ro.getSystemSign() == "") {
                ro.setSystemSign(null);
            }
            if (ro.getAccountType() == null || ro.getAccountType() == "") {
                ro.setAccountType(null);
            }
            if (StringUtils.isNotBlank(ro.getCreateTimeEnd())) {
                countEndDay = 0;
            } else {
                countEndDay = accountInfoStatisticMapper.getTodaySumByAll(ro.getSystemSign(),ro.getAccountType(),ro.getAccountStatus(), nextDate, createTimeEnd);
            }
            if (StringUtils.isNotBlank(ro.getCreateTimeStart())) {
                String startDate = StatsUtils.getPastDate(1, createTimeStart.substring(0, 10));
                countStartSum = accountInfoStatisticMapper.getPastSum(ro.getSystemSign(),ro.getAccountType(),ro.getAccountStatus(), startDate);
            }
            String countEndSum = accountInfoStatisticMapper.getPastSum(ro.getSystemSign(),ro.getAccountType(),ro.getAccountStatus(), endDate);
            if (countEndSum == null) {
                countEndSum = "0";
            }
            if (countStartSum == null) {
                countStartSum = "0";
            }
            //如果没选择开始时间和结束时间则把时间为空的数据给补上
            String countTimeNull = "0";
            if (StringUtils.isBlank(createTimeStart) && StringUtils.isBlank(createTimeEnd)){
                countTimeNull  = accountInfoStatisticMapper.getPastSum(ro.getSystemSign(),ro.getAccountType(),ro.getAccountStatus(), "0000-00-00");
            }
            if (countTimeNull == null) {
                countTimeNull = "0";
            }
            count = Integer.parseInt(countEndSum) + countEndDay + Integer.parseInt(countTimeNull) - Integer.parseInt(countStartSum);
            Integer startPage = (pageNo - 1) * pageSize;
            pos = customerAccountInfoMapper.getListByPage(ro,startPage,pageSize);
        }else{
            Page<CustomerAccountPo> page = PageHelper.startPage(pageNo, pageSize);
            pos = customerAccountInfoMapper.getList(ro);
            count = (int) page.getTotal();
        }
        CustomerAccountVo vo;
        for (CustomerAccountPo po : pos) {
            vo = new CustomerAccountVo();
            BeanUtils.copyProperties(po, vo);
            vo.setCreateTime(CustomerUtils.timestamp2Stirng(po.getCreateTime()));
            vo.setUpdateTime(CustomerUtils.timestamp2Stirng(po.getUpdateTime()));
            vo.setAccountCreateTime(CustomerUtils.timestamp2DateString(po.getAccountCreateTime()));
            vo.setAccountUpdateTime(CustomerUtils.timestamp2DateString(po.getAccountUpdateTime()));
            vo.setAccountStatusStr(getAccountStatusStr(String.valueOf(po.getAccountStatus())));
            vo.setAccountTypeStr(getAccountTypeStr(po.getAccountType()));
            vo.setSystemSignStr(cacheService.getSystemNameBySign(po.getSystemSign()));
            vo.setSubSystemSignStr(cacheService.getSystemNameBySign(po.getSubSystemSign()));
            vos.add(vo);
        }
        return new PageInfo(vos, count, pageNo, pageSize);
    }

    private String getAccountTypeStr(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        Map<String, String> contactTypeMap = DictionaryFactory.Dict.ACCOUNT_TYPE.getDictMap();
        return contactTypeMap.get(key);
    }

    private String getAccountStatusStr(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        Map<String, String> contactTypeMap = DictionaryFactory.Dict.ACCOUNT_STATUS.getDictMap();
        return contactTypeMap.get(key);
    }

}
